﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LX.Core.Utils;
using LX.Web.Mvc;
using S4Store.Service;
using S4Store.Domain;
using S4Store.Models;
using S4Store.Models.AfterSaleCarKPI;

namespace S4Store.Controllers
{
    public class AfterSaleWorkshopManagerController : ReportBaseController
    {
        //
        // GET: /AfterSaleServiceAdvisor/
        public S4Store.Service.EmployeeService EmployeeService { get; set; }
        public S4Store.Service.AfterSaleCarKPIService AfterSaleCarKPIService { get; set; }
        //public S4Store.Service.
        public ActionResult Index()
        {
            WorkshopManagersIndexModel model = new WorkshopManagersIndexModel();
            using (var t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                t.Inject(EmployeeService);
                int[] employs = EmployeeService.GetEnableEmployee(Store.Id, EmployeeType.WorkshopSupervisor).ToList().Select(o => o.Id).ToArray();
                var data = AfterSaleCarKPIService.GetAfterSaleWorkshopManagerDayInputQuery().Where(x => employs.Contains(x.EmployeeId)).ToList();
                model.MonthDayData = data;
                return View(model);
            }
        }

        public ActionResult MonthConfig(int y, int m)
        {
            using (Transtion t = new Transtion())
            {
                t.Inject(EmployeeService);
                t.Inject(AfterSaleCarKPIService);
                WorkshopManagersListModel model = new WorkshopManagersListModel();
                model.Date = new DateTime(y, m, 1);
                model.Employees = EmployeeService.GetEnableEmployee(Store.Id, EmployeeType.WorkshopSupervisor).ToList();
                model.MonthConfigs = AfterSaleCarKPIService.GetAfterSaleWorkshopManagerMonthConfigs(y, m, model.Employees.Select(o => o.Id).ToArray());
                model.isconfirmed = model.MonthConfigs.Where(o => o.IsConfirmed).Count() > 0;
                return View(model);
            }
        }

        public JsonResult ModifyBackInputWorkshopManagerMonthConfig(List<Domain.AfterSaleKpi.AfterSaleWorkshopManagerMonthConfig> afterSaleCarlist, int y, int m, bool isconfirm = false)
        {
            using (var t = new Transtion())
            {
                t.Inject(EmployeeService);
                t.Inject(AfterSaleCarKPIService);
                DateTime now = new DateTime(y, m, 1);
                var list = new List<S4Store.Domain.AfterSaleKpi.AfterSaleWorkshopManagerMonthConfig>();
                foreach (var model in afterSaleCarlist)
                {
                    model.AddTime = now;
                    model.IsConfirmed = isconfirm;
                    list.Add(model);
                }
                AfterSaleCarKPIService.SubmitWorkshopManagerMonthConfigsList(now.Year, now.Month, EmployeeService.GetEnableEmployee(Store.Id, EmployeeType.WorkshopSupervisor).ToList().Select(o => o.Id).ToArray(), list);
                t.Commit();
            }

            return new JsonResult
            {
                Data = new
                {
                    success = true
                }
            };
        }

        public ActionResult MonthTarget(int y, int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(EmployeeService);
                t.Inject(AfterSaleCarKPIService);
                int[] employs = EmployeeService.GetEnableEmployee(Store.Id, EmployeeType.WorkshopSupervisor).ToList().Select(o => o.Id).ToArray();
                var config = AfterSaleCarKPIService.GetAfterSaleWorkshopManagerMonthConfigs(y, m, employs).FirstOrDefault();
                if (config == null)
                {
                    config = new Domain.AfterSaleKpi.AfterSaleWorkshopManagerMonthConfig();
                    config.AddTime = new DateTime(y, m, 1);
                    config.Id = 0;
                }
                return View(config);
            }
        }

        public ActionResult DayInput(int y, int m, int d)
        {
            using (Transtion t = new Transtion())
            {
                t.Inject(EmployeeService);
                t.Inject(AfterSaleCarKPIService);
                WorkshopManagersListModel model = new WorkshopManagersListModel();
                model.Date = new DateTime(y, m, d);
                model.Employees = EmployeeService.GetEnableEmployee(Store.Id, EmployeeType.WorkshopSupervisor).ToList();
                model.DayInputs = AfterSaleCarKPIService.GetAfterSaleWorkshopManagerDayInputs(y, m, d, model.Employees.Select(o => o.Id).ToArray());
                model.isconfirmed = model.DayInputs.Where(o => o.IsConfirmed).Count() > 0;
                return View(model);
            }
        }

        public JsonResult ModifyBackInputWorkshopManagerDayInput(List<Domain.AfterSaleKpi.AfterSaleWorkshopManagerDayInput> afterSaleCarlist, int y, int m, int d, bool isconfirm = false)
        {
            using (var t = new Transtion())
            {
                t.Inject(EmployeeService);
                t.Inject(AfterSaleCarKPIService);
                DateTime now = new DateTime(y, m, d);
                var list = new List<S4Store.Domain.AfterSaleKpi.AfterSaleWorkshopManagerDayInput>();
                foreach (var model in afterSaleCarlist)
                {
                    model.AddTime = now;
                    model.IsConfirmed = isconfirm;
                    list.Add(model);
                }
                AfterSaleCarKPIService.SubmitWorkshopManagerDayInputsList(y, m, d, EmployeeService.GetEnableEmployee(Store.Id, EmployeeType.WorkshopSupervisor).ToList().Select(o => o.Id).ToArray(), list);
                t.Commit();
            }

            return new JsonResult
            {
                Data = new
                {
                    success = true
                }
            };
        }

    }
}
